﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class KHACHHANG_Control : IDisposable
    {

        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.KHACHHANG_Get_List();
            var typeorder = types.OrderBy(w => w.TEN_KHACHHANG);
            foreach (var t in typeorder)
            {                
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_KHACHHANG";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            KHACHHANG_Get_ListResult temp = new KHACHHANG_Get_ListResult();
            temp.MA_KHACHHANG = "...";
            temp.TEN_KHACHHANG = "...";
            temp.DIEN_THOAI = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.KHACHHANG_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "KH00000001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "KH";
                    for (int j = 0; j < (8 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.KHACHHANG_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_KHACHHANG.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_kh, ComboBox Nhom_KH, TextBox txtTen_KH, CheckBox chbThuong_Xuyen, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtDi_Dong, TextBox txtEmail, TextBox txtWebSite, TextBox txtFax, TextBox txtMST, TextBox txtNguoi_DD, TextBox txtTen_TK, TextBox txtSo_TK, TextBox txtTen_NganHang, TextBox txtDC_NganHang, RichTextBox txtGhi_Chu)
        {
            if (validateData( Nhom_KH,  txtTen_KH,  txtDia_Chi,  txtDi_Dong) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmb1 = (NHOM_KH_Get_ListResult)Nhom_KH.SelectedItem;
                db.KHACHHANG_Insert(ma_kh, cmb1.MA_NHOM_KH, txtTen_KH.Text, chbThuong_Xuyen.IsChecked, txtDia_Chi.Text, txtD_Thoai.Text, txtDi_Dong.Text, txtEmail.Text, txtWebSite.Text, txtFax.Text, txtMST.Text, txtNguoi_DD.Text, txtTen_TK.Text, txtSo_TK.Text, txtTen_NganHang.Text, txtDC_NganHang.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(ComboBox Nhom_KH, TextBox txtTen_KH, TextBox txtDia_Chi, TextBox txtDi_Dong)
        {
            if (txtTen_KH.Text.Trim() == "" || Nhom_KH.SelectedItem == null || txtDia_Chi.Text.Trim() == "" || txtDi_Dong.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.KHACHHANG_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_KHACHHANG);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }

        public void loadData(DataPager dp)
        {

            var list = db.KHACHHANG_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_KHACHHANG);
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {
                if (t.TEN_KHACHHANG.Trim() != "")
                {
                    dpSource.Add(new { STT = i, t.DC_NGANHANG, t.DI_DONG, t.DIA_CHI, t.DIEN_THOAI, t.EMAIL, t.FAX, t.GHI_CHU, t.MA_KHACHHANG, t.MA_NHOM_KH, t.MST, t.NGUOI_DD, t.SO_TK, t.TEN_KHACHHANG, t.TEN_NGANHANG, t.TEN_TK, t.THUONG_XUYEN, t.WEBSITE });
                    i++;               
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string ma_kh, ComboBox Nhom_KH, TextBox txtTen_KH, CheckBox chbThuong_Xuyen, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtDi_Dong, TextBox txtEmail, TextBox txtWebSite, TextBox txtFax, TextBox txtMST, TextBox txtNguoi_DD, TextBox txtTen_TK, TextBox txtSo_TK, TextBox txtTen_NganHang, TextBox txtDC_NganHang, RichTextBox txtGhi_Chu)
        {
            if (validateData(Nhom_KH, txtTen_KH, txtDia_Chi, txtDi_Dong) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmb1 = (NHOM_KH_Get_ListResult)Nhom_KH.SelectedItem;
                db.KHACHHANG_Update(ma_kh, ma_kh, cmb1.MA_NHOM_KH, txtTen_KH.Text, chbThuong_Xuyen.IsChecked, txtDia_Chi.Text, txtD_Thoai.Text, txtDi_Dong.Text, txtEmail.Text, txtWebSite.Text, txtFax.Text, txtMST.Text, txtNguoi_DD.Text, txtTen_TK.Text, txtSo_TK.Text, txtTen_NganHang.Text, txtDC_NganHang.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.PHIEUXUAT_GetByMaKhachhang(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.KHACHHANG_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;


        }
        NHOM_KH_Control ctrl1 = new NHOM_KH_Control();
        public void loadForm_Edit(string ID_Edit,  ComboBox Nhom_KH, TextBox txtTen_KH, CheckBox chbThuong_Xuyen, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtDi_Dong, TextBox txtEmail, TextBox txtWebSite, TextBox txtFax, TextBox txtMST, TextBox txtNguoi_DD, TextBox txtTen_TK, TextBox txtSo_TK, TextBox txtTen_NganHang, TextBox txtDC_NganHang, RichTextBox txtGhi_Chu)
        {

            var sl = (KHACHHANG_GetByMaKhachhangResult)db.KHACHHANG_GetByMaKhachhang(ID_Edit).Single();


            

            ctrl1.loadComboBox(Nhom_KH);
            int t = 0;
            foreach (var i in Nhom_KH.Items)
            {
                var temp = (NHOM_KH_Get_ListResult)i;
                if (temp.MA_NHOM_KH == sl.MA_NHOM_KH)
                {
                    break;
                }
                t++;
            }
            Nhom_KH.SelectedIndex = t;

            txtTen_KH.Text = sl.TEN_KHACHHANG;
            chbThuong_Xuyen.IsChecked = sl.THUONG_XUYEN;
            txtDia_Chi.Text = sl.DIA_CHI;
            txtD_Thoai.Text = sl.DIEN_THOAI;
            txtDi_Dong.Text = sl.DI_DONG;
            txtEmail.Text = sl.EMAIL;
            txtWebSite.Text = sl.WEBSITE;
            txtFax.Text = sl.FAX;
            txtMST.Text = sl.MST;
            txtNguoi_DD.Text = sl.NGUOI_DD;
            txtTen_TK.Text = sl.TEN_TK;
            txtSo_TK.Text = sl.SO_TK;
            txtTen_NganHang.Text = sl.TEN_NGANHANG;
            txtDC_NganHang.Text = sl.DC_NGANHANG;
            txtGhi_Chu.AppendText(  sl.GHI_CHU);
        }

        
    }
}
